package com.wanxi.education.config;

import com.wanxi.education.filter.JwtAuthenticationTokenFilter;
import com.wanxi.education.filter.MyAccessDenidHandler;
import com.wanxi.education.filter.RestAuthenticationEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @Author: Kang
 * @Date: 2021/9/24 11:03
 * @Description: 权限认证配置类
 * @Version: 1.0
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) //开启方法级别安全控制
public class WebSecurityConfig  extends WebSecurityConfigurerAdapter {

    @Autowired
    @Qualifier("userService")
    private UserDetailsService userDetailsService;

    @Autowired
    private MyAccessDenidHandler myAccessDenidHandler;

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Autowired
    private RestAuthenticationEntryPoint restAuthenticationEntryPoint;

//    基于内存创建用户
    /*@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .passwordEncoder(passwordEncoder())
                .withUser("admin")
                .password(passwordEncoder().encode("123456")).roles("admin");
    }*/

//    交互式
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//      设置不拦截页面，可直接访问
        http.authorizeRequests()
//                设置允许访问的资源路径
                .antMatchers("/**").permitAll()
                .antMatchers("/front/CUser/account/changePassword").permitAll()
                .antMatchers("/back/admin/login").permitAll()
                .anyRequest().authenticated() //其余接口必须登录才可以访问
                .and()
                .userDetailsService(userDetailsService)
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); //Security永久不适用Session

        http.csrf().disable();

//        登录拦过滤器
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

//        异常处理 401 403
        http.exceptionHandling()
                .accessDeniedHandler(myAccessDenidHandler)
                .authenticationEntryPoint(restAuthenticationEntryPoint);

    }

    /**
     * 注入密码加密
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

}
